package com.qfedu.demo;

import com.qfedu.bean.User;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class Demo01 {
    public static void main(String[] args) {


        Connection connection=null;
        PreparedStatement statement=null;
        ResultSet resultSet =null;
        try {
            //1,加载驱动
            Class.forName("com.mysql.jdbc.Driver");
            //2,获取连接
            connection= DriverManager.getConnection("jdbc:mysql://localhost:3306/day001","root","admin123");
            //3,编写sql，获取执行sql对象
             statement=connection.prepareStatement("select * from tb_user");
            //4,执行sql

            resultSet=statement.executeQuery();
            //5,处理结果集

            while (resultSet.next()){
                List<User> userList = new ArrayList<User>();
                Integer id = resultSet.getInt("id");
                String username = resultSet.getString("username");
                String password = resultSet.getString("password");
                System.out.println("id:"+id+" username:"+username+" password:"+password);
//                System.out.println(id+username+password);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        //6,释放资源
        if (null!=connection){
            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            connection=null;
        }
        if (null!=statement){
            try {
                statement.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            statement=null;
        }
        if (null!=resultSet){
            try {
                resultSet.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            resultSet=null;
        }
    }
}
